﻿package net.ssta.core.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

public class PropertiesConfig extends Config
{
	private Properties props ;
	protected PropertiesConfig(String file) throws ConfigException
	{
		super(file);
	}
	
	@Override
	public String[] getPropertyNames()
	{
		Iterator it = props.keySet().iterator();
		String[] names = new String[props.size()];
		for(int i = 0; it.hasNext(); i++)
		{
			names[i] = (String)it.next();
		}
		return names;
	}

	@Override
	protected String getValue(String key)
	{
		return props == null ? null : props.getProperty( key );
	}
	
	@Override
	protected boolean load(File configFile) throws ConfigException
	{
		boolean bRet = false;
		InputStream in = null;
		try 
		{
			in = new FileInputStream(getConfigFile());
			Properties properties = new Properties();
			properties.load(in);//从文件流中加载配置
			props = properties;
			bRet = true;
		}
		catch(FileNotFoundException e)
		{
			throw new ConfigException(e);
		}
		catch (IOException e)
		{
			throw new ConfigException(e);
		}
		finally
		{
			if(in!=null)
			{
				try 
				{
					in.close();
				}
				catch (IOException e) 
				{
				}
				catch (Exception e) 
				{
				}
			}
		}
		return bRet;
	}
}
